// pages/module/proManagement/proManagement.js
import regeneratorRuntime from '../../../utils/runtime.js';
import img_list from '../../../utils/util.js';
import {
	getData,
	setData,
	clearData,
	dataKey
} from '../../../utils/storage.js';
import {
	getLiveProduct,
	addProductToLive,
	loadLiveGoods
} from '../../../utils/imapi.js';
var app = getApp()
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		tabIndex: 0,
		width: 400,
		sliderLeft: 103,
		sliderOffset: 120,
		top: app.globalData.CustomBar,
		tabs: [{
				"id": 2,
				"name": "已审核",
				"is_selected": 1
			},
			{
				"id": 0,
				"name": "未提交",
				"is_selected": 1
			},
			{
				"id": 1,
				"name": "审核中",
				"is_selected": 1
			},

			{
				"id": 3,
				"name": "未通过",
				"is_selected": 1
			},
		],
		top: app.globalData.CustomBar,
		is_confirm: false,
		dotList: [],
		img_list: "img_list.img_list",
		tab_index: 0,
		page_index:1,
		productList:[]
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: async function(options) {
		this.setData({
			img_list: img_list.img_list
		})
		this.data.tab_index = 0;
		this.page = 1;
		this.canLoadMore = true;
		// 直接先展示已审核的
		this.loadGoods('all','2')
	},
	async onTabClick(e) {	
		if(!e.detail){
			var productDatas = await getLiveProduct({
				audit_status: this.data.tabs[this.data.tab_index].id
			}) || {}
			this.setData({
				productList: productDatas.data,
			})
			return;
		};
			var index = e.detail.index;
			this.data.tab_index = index;
			this.setData({
				tab_index: e.detail.index
			})
			console.log(typeof this.data.tab_index);
			// if(this.data.tab_index==0) {
			// 	this.loadGoods('all','2')
			// 	return
			// }
			// if(this.data.tab_index==2) {
			// 	this.loadGoods('all','1')
			// 	return
			// }
			// if(this.data.tab_index==3) {
			// 	this.loadGoods('all','3')
			// 	return
			// }
			this.loadGoods('all',this.data.tabs[this.data.tab_index].id.toString())
			
	},
	async loadGoods(p='all',status,pagesize=10,pagenum=1) {
		var productDatas = await loadLiveGoods({
			p:p,
			status: status,
			page_index:pagenum,
			page_size:pagesize
		}) || {}
		console.log(productDatas);
		this.setData({
			productList: productDatas.data,
		})
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},
	aa() {
		console.log('11');
	},
	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {
		wx.showNavigationBarLoading({})
		setTimeout(function() {
			wx.hideNavigationBarLoading({})
		}, 600)
		this.setData({
			page_index:1
		})
		// var productDatas = await getLiveProduct({
		// 	status: this.data.tabs[this.data.tab_index].id,
		// 	page_index:this.data.page_index++
		// }) || {}
		// this.setData({
		// 	productList: productDatas.data,
		// })
		this.loadGoods('all',this.data.tabs[this.data.tab_index].id.toString())
	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: async function() {
		if (!this.canLoadMore) return;
		wx.showLoading({
			title: this.page_index == 1 ? '加载中...' : '加载更多...',
		});
		console.log(typeof this.data.tabs[this.data.tab_index].id);
		var {
			data
		} = await loadLiveGoods({
			status: '2',
			p:'all',
			page_index: this.data.page_index++,
			page_size:10
		}) || {}
		console.log(data);
		let arr = data;
		console.log(data);
		if (arr.length > 0) {
			this.setData({
				page_index:this.data.page_index++
			})
		const newarr=	this.data.productList.push(...arr)
		console.log(newarr);
			this.setData({
				// productList:[...this.data.productList, ...arr],
				productList: this.data.productList
			})
		} else {
			wx.showToast({
				icon:'none',
				title:'全部商品已展示完成,请再去添加'
			})
			this.canLoadMore = false;
		}
		wx.hideLoading({})
	},

	select_quanwang(){ //全网默认搜索佣金205 以上的 有券的

		wx.navigateTo({
			url: '/pages/module/search/searchResult/searchResult?tabIndex=' + 0  + '&commission_rate_from=' + 0.2 +'&is_live=1'+'&is_coupon='+1,
		})

	},
	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
